package top.huxy.boot.service.impl;

import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.huxy.boot.mapper.BlogMapper;
import top.huxy.boot.model.dto.*;
import top.huxy.boot.model.entity.Blog;
import top.huxy.boot.model.entity.User;
import top.huxy.boot.model.vo.*;
import top.huxy.boot.service.BlogService;

import java.util.List;

@Service
@Slf4j
public class BlogServiceImpl implements BlogService {

    @Autowired
    BlogMapper blogMapper;

    @Autowired
    TagServiceImpl tagService;

    @Override
    @Transactional
    public Integer addBlog(DTOBlog dtoBlog, int adminId) {
        Blog blog = new Blog(dtoBlog, adminId);

        //保存博客数据
        blogMapper.saveBlog(blog);

        //取出刚插入的博客id
        int blogId = blogMapper.selectMaxId();

        //标签数据
        List<String> tagList = dtoBlog.getTags();


        if (tagList != null) {
            tagService.changeBlogTag(blogId, tagList);
        }
        return 1;
    }

    @Override
    public List<VOBlog> getBlogList(Pager pager) {
        PageHelper.startPage(pager.getPage(), pager.getPageSize(), "create_time desc");
        List<VOBlog> publicBlogList = blogMapper.getPublicBlogList();
        PageHelper.clearPage();

        for (VOBlog voBlog : publicBlogList) {
            List<String> tagList = tagService.getTagsNameByBlogId(voBlog.getId());
            voBlog.setTags(tagList);
        }
        return publicBlogList;
    }

    @Override
    public VOBlog getBlogById(Integer id) {
        return blogMapper.getBlogById(id);
    }

    @Override
    public void replyComment(DTOReply reply, User user) {

    }

    @Override
    public void addComment(DTOComment comment, User user) {

    }

    @Override
    public List<VOComment> getCommentList(Integer blogID) {
        return null;
    }

    @Override
    public List<VOReply> getCommentReplyList(Integer commentId) {
        return null;
    }

    @Override
    public int getRidByCid(Integer rid) {
        return 0;
    }

    @Override
    public int replyReply(int cid, DTOReplyReply reply, User user) {
        return 0;
    }

    @Override
    public List<VOBlogQuery> selectBlogByTitle(String title) {
        return blogMapper.selectBlogByTitle(title);
    }

    @Override
    public VOBlogDetails getBlogDetailById(Integer id) {
        VOBlogDetails blogDetailById = blogMapper.getBlogDetailById(id);
        List<String> tagsNameByBlogId = tagService.getTagsNameByBlogId(blogDetailById.getId());
        blogDetailById.setTags(tagsNameByBlogId);
        return blogDetailById;
    }

    @Override
    @Transactional
    public int updateBlog(DTOBlog dtoBlog, Integer id) {
        //更新博客内容
        blogMapper.updateBlog(dtoBlog);

        //更新博客标签
        tagService.changeBlogTag(dtoBlog.getId(), dtoBlog.getTags());
        return 1;
    }

    @Override
    public List<VOBlog> getAdminBlogList() {
        return blogMapper.getAdminBlogList();
    }

    @Override
    public int getPublicBlogCount() {
        return blogMapper.getPublicBlogCount();
    }

    @Override
    public int getProtectBlogCount() {
        return blogMapper.getProtectBlogCount();
    }

    @Override
    public int getPrivateBlogCount() {
        return blogMapper.getPrivateBlogCount();
    }

    @Override
    public int getTopBlogCount() {
        return blogMapper.getTopBlogCount();
    }
}
